<?
	class Database {
		var $DBName;
		var $DBType;

		var $Connect;
		var $Query;
		var $Result;

		var $Error_message_1;
		var $Error_message_2;
		var $Error_message_3;

		function Connect($getHost, $getDb, $getId = '1234', $getPass = '1234', $type = 'mysql', $port='', $option_str='') {
			$this->DBName = $getDb;
			$this->DBType = strtolower($type);

			if($this->DBType=='mysql') {
				$this->Connect=mysql_connect($getHost, $getId, $getPass) or die(mysql_error());
				mysql_select_db($getDb, $this->Connect) or die(mysql_error());
				mysql_query('set names utf8');
			} elseif($this->DBType=='mssql') {
				$this->Connect=mssql_connect($getHost, $getId, $getPass);
				mssql_select_db($getDb, $this->Connect);
			} elseif($this->DBType=='cubrid') {
				if(!$port) {
					$port=33000;
				}
				$this->Connect=cubrid_connect($getHost, $port, 'sample', $getId, $getPass);
			} elseif($this->DBType=='pgsql') {
				if(!$port) {
					$port=5432;
				}
				$this->Connect = pg_connect('host='.$getHost.' port='.$port.' dbname='.$getDb.' user='.$getId.' password='.$getPass);
			} elseif($this->DBType=='oracle') {
				if(!$port) {
					$port='1521';
				}
				$this->Connect = oci_connect($getId, $getPass, $getHost.'/'.$getDb, 'AL32UTF8');
			} elseif($this->DBType=='sqlite') {
				if(!$getDb) {
					$getDb = '0666';
				}
				$this->Connect = sqlite_open($getHost, $getDb, $this->sqliteerror); 
			} elseif($this->DBType=='sqlite3') {
				if(!$getDb) {
					$getDb = '0666';
				}
				$this->Connect = new SQLite3($getHost); 
			}
			global $bdb_conn;
			$bdb_conn = $this->Connect;
			return $this->Connect;
		}

		function Query($query, $getcon='') {
			global $conn;
			if($getcon) {
				$dbcon = $getcon;
			} else {
				$dbcon = $conn;
			}

			$this->Query = $query;
			if($this->DBType=='mysql') {
				$exists = @mysql_query($query, $dbcon) or die(die(mysql_error()));
			} else if($this->DBType=='mssql') {
				$exists = mssql_query($query, $dbcon) or die(mssql_get_last_message());
			} else if($this->DBType=='pgsql') {
				$exists = pg_query($dbcon, $query) or die(pg_last_error($dbcon));
			} else if($this->DBType=='cubrid') {
				$exists = cubrid_execute($this->Connect, $query);
				if(!$exists) {
					die(cubrid_errno($dbcon).':'.cubrid_error($con));
				}
			} else if($this->DBType=='oracle') {
				$stmt = oci_parse($this->Connect, $query);
				if (!$stmt) {
					$e = oci_error($this->Connect);  // For oci_parse errors pass the connection handle
					trigger_error(htmlentities($e['message']), E_USER_ERROR);
					exit;
				} else {
					if(ociexecute($stmt, OCI_DEFAULT)) {
						$exists = $stmt;
					} else {
						$e = oci_error($stmt); 
						echo 'error : '.$query.' '.$e['message'];
						exit;
					}
				}
			} else if($this->DBType=='sqlite') {
				$exists = sqlite_query($this->Connect, $query);
				echo $this->sqliteerror;
				if(!$exists) {
					die($this->sqliteerror);
				}
			} else if($this->DBType=='sqlite3') {
				$exists = $this->Connect->query($query);
				if(!$exists) {
					die($this->Connect);
				}
			}
			if($exists) {
				$this->Result = $exists;
				return $exists;
			}
			return false;
		}

		function Fetch(&$result, $assoc=false) {
			if($result) {
				if($this->DBType=='mysql') {
					if($assoc) { 
						$exists= @mysql_fetch_assoc($result);
					} else {
						$exists= @mysql_fetch_array($result);
					}
					if($exists) return $exists;
				} else if($this->DBType=='mssql') {
					if($assoc) {
						$exists= @mssql_fetch_array($result);
					} else {
						$exists= @mssql_fetch_assoc($result);
					}
					if($exists) return $exists;
				} else if($this->DBType=='pgsql') {
					if(!$assoc) {
						$assoc = PGSQL_BOTH;
					} else {
						$assoc = PGSQL_ASSOC;
					}
					$exists= @pg_fetch_array($result);
					if($exists) return $exists;
				} else if($this->DBType=='cubrid') {
					if(!$assoc) {
						$assoc = CUBRID_BOTH;
					} else {
						$assoc = CUBRID_ASSOC;
					}
					$exists= @cubrid_fetch($result, $assoc);
					if($exists) return $exists;
				} else if($this->DBType=='oracle') {
					if(!$assoc) {
						$assoc = OCI_BOTH;
					} else {
						$assoc = OCI_ASSOC;
					}
					$exists = oci_fetch_array($result, $assoc+OCI_RETURN_LOBS);
					if($exists) {
						// strtolower key name
						foreach($exists as $key => $value) {
							$exists2[strtolower($key)] = $value;
						}
						return $exists2;
					}
				} else if($this->DBType=='sqlte') {
					if(!$assoc) {
						$assoc = SQLITE_BOTH;
					} else {
						$assoc = SQLITE_ASSOC;
					}
					$exists= @sqlite_fetch_array ($result, $assoc);
					if($exists) return $exists;
				} else if($this->DBType=='sqlte3') {
					if(!$assoc) {
						$assoc = SQLITE3_BOTH;
					} else {
						$assoc = SQLITE3_ASSOC;
					}
					$exists= @sqlite_fetch_array ($result, $assoc);
					if($exists) return $exists;
				}
			}
			return false;
		}

		function Begin() {
			if($this->DBType=='mysql') {
				$this->Query('begin');
			} else if($this->DBType=='mssql') {
				$this->Query('begin tran');
			} else if($this->DBType=='pgsql') {
				$this->Query('begin');
			} else if($this->DBType=='cubrid') {
				$this->Query('begin');
			} else if($this->DBType=='oracle') {
				//$this->Query('begin');
			} else if($this->DBType=='sqlite') {
				$this->Query('begin');
			} else if($this->DBType=='sqlite3') {
				$this->Query('begin');
			}

		}

		function Commit() {
			if($this->DBType=='mysql') {
				$this->Query('commit');
			} else if($this->DBType=='mssql') {
				$this->Query('commit');
			} else if($this->DBType=='pgsql') {
				$this->Query('commit');
			} else if($this->DBType=='cubrid') {
				$this->Query('commit');
			} else if($this->DBType=='oracle') {
				$this->Query('commit');
			} else if($this->DBType=='sqlite') {
				$this->Query('commit');
			} else if($this->DBType=='sqlite3') {
				$this->Query('commit');
			}

		}

		function Rollback() {
			if($this->DBType=='mysql') {
				$this->Query('rollback');
			} else if($this->DBType=='mssql') {
				$this->Query('rollback');
			} else if($this->DBType=='pgsql') {
				$this->Query('rollback');
			} else if($this->DBType=='cubrid') {
				$this->Query('rollback');
			} else if($this->DBType=='oracle') {
				$this->Query('rollback');
			} else if($this->DBType=='sqlite') {
				$this->Query('rollback');
			} else if($this->DBType=='sqlite3') {
				$this->Query('rollback');
			}


		}

		function QueryFetch($query) {
			return $this->Fetch($this->Query($query));
		}

		function QueryFetchList($query) {
			$data_r = $this->Query($query);
			while($data_d=$this->Fetch($data_r)) {
				$data_l[] = $data_d;
			}
			return $data_l;
		}

		function Excute($query) {
				return $this->Query($query);
		}

		// Query Class wrapped
		function Select(&$qry) {
			return $this->Query($qry->SelectQuery());
		}

		function Delete(&$qry) {
			return $this->Query($qry->DeleteQuery());
		}

		function Update(&$qry) {
			return $this->Query($qry->UpdateQuery());
		}
	}
?>